﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SilverlightSdms
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            App app = (App)Application.Current;

            InitializeComponent();

            Link2.Visibility = System.Windows.Visibility.Collapsed;
            Link3.Visibility = System.Windows.Visibility.Collapsed;
            Link4.Visibility = System.Windows.Visibility.Collapsed;
            Link5.Visibility = System.Windows.Visibility.Collapsed;
            Link6.Visibility = System.Windows.Visibility.Collapsed;
            Divider2.Visibility = System.Windows.Visibility.Collapsed;
            Divider3.Visibility = System.Windows.Visibility.Collapsed;
            Divider4.Visibility = System.Windows.Visibility.Collapsed;
            Divider5.Visibility = System.Windows.Visibility.Collapsed;

            app.IsUserAuthorizedChanged += new IsUserAuthorizedChangedEventHandler(app_IsUserAuthorizedChanged);
        }

        void app_IsUserAuthorizedChanged(object sender, EventArgs e)
        {
            if (((App)Application.Current).IsUserAuthorized)
            {
                LinksStackPanel.Visibility = System.Windows.Visibility.Visible;
                Link6.Visibility = System.Windows.Visibility.Visible;
                foreach (var role in ((App)Application.Current).RoleTypes)
                {
                    bool isAllLinkAlreadyVisible = false;

                    switch (role)
                    {
                        case eRoleType.Administrator:
                            Link2.Visibility = System.Windows.Visibility.Visible;
                            Link3.Visibility = System.Windows.Visibility.Visible;
                            Link4.Visibility = System.Windows.Visibility.Visible;
                            Link5.Visibility = System.Windows.Visibility.Visible;
                            Divider2.Visibility = System.Windows.Visibility.Visible;
                            Divider3.Visibility = System.Windows.Visibility.Visible;
                            Divider4.Visibility = System.Windows.Visibility.Visible;
                            Divider5.Visibility = System.Windows.Visibility.Visible;
                            isAllLinkAlreadyVisible = true;
                            break;
                        case eRoleType.Doctor:
                            Link2.Visibility = System.Windows.Visibility.Visible;
                            Divider2.Visibility = System.Windows.Visibility.Visible;
                            break;
                        case eRoleType.FSM:
                            Link4.Visibility = System.Windows.Visibility.Visible;
                            Divider4.Visibility = System.Windows.Visibility.Visible;

                            break;
                        case eRoleType.TeamLeader:
                            Link3.Visibility = System.Windows.Visibility.Visible;
                            Divider3.Visibility = System.Windows.Visibility.Visible;
                            break;
                    }

                    if (isAllLinkAlreadyVisible)
                    {
                        break;
                    }

                }
            }
            else
            {
                Link2.Visibility = System.Windows.Visibility.Collapsed;
                Link3.Visibility = System.Windows.Visibility.Collapsed;
                Link4.Visibility = System.Windows.Visibility.Collapsed;
                Link5.Visibility = System.Windows.Visibility.Collapsed;
                Link6.Visibility = System.Windows.Visibility.Collapsed;
                Divider2.Visibility = System.Windows.Visibility.Collapsed;
                Divider3.Visibility = System.Windows.Visibility.Collapsed;
                Divider4.Visibility = System.Windows.Visibility.Collapsed;
                Divider5.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
        }

        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

    }
}